﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common.Entity;
using ESOL.Business.Fa;
using ESOL.Common;

namespace ESOL.WinUI.Fa
{
    public partial class frmMaintainAssetMaintenance : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        private FaManufacturerEntity mSelectedEntity;
        private bool mIsNew = true;
        #endregion

        #region Construction
        public frmMaintainAssetMaintenance(FaAssetsEntity asset)
        {

            InitializeComponent();
            if (asset != null)
            {
                mSelectedEntity = FaManufacturerBs.GetByID(asset.AssetId);
                if (mSelectedEntity == null)
                {
                    mSelectedEntity = new FaManufacturerEntity();
                    mSelectedEntity.AssetId = asset.AssetId;
                    mIsNew = true;
                }
                else
                {
                    mIsNew = false;
                }
            }
            else
            {
                mSelectedEntity = new FaManufacturerEntity();
                mIsNew = true;
            }
        }

        //public frmMaintainAssetMaintenance(string assetId)
        //{
        //    InitializeComponent();
        //    mSelectedEntity = FaManufacturerBs.GetByID(assetId);
        //    if (mSelectedEntity == null)
        //    {
        //        mSelectedEntity = new FaManufacturerEntity();
        //        mSelectedEntity.AssetId = assetId;
        //        mIsNew = true;
        //    }
        //    else
        //    {
        //        mIsNew = false;
        //    }
        //}
        #endregion

        #region Function
        private void SetFields()
        {
            if (string.IsNullOrEmpty(mSelectedEntity.AssetId) == false)
            {
                FaAssetsEntity fa = FaAssetsBs.GetByID(mSelectedEntity.AssetId);
                txtAsset.Text = fa.AssetCode + " (" + fa.AssetName + ")";
                txtAsset.Tag = fa;
            }
            if (ThisActionType == ActionType.New)
            {
                return;
            }
            if (mIsNew)
            {
                return;
            }
            txtLastMaintainPerson.Text = mSelectedEntity.LastMaintainPerson;
            txtLastMaintainItem.Text = mSelectedEntity.LastMaintainItem;
            txtLastMaintainFee.Text = mSelectedEntity.LastMaintainFee.ToString("#0.00");
            txtLastMaintainCompany.Text = mSelectedEntity.LastMaintainCompany;
            txtRemark.Text = mSelectedEntity.Remark;
            if (mSelectedEntity.LastMaintainDate.ToString("yyyy-MM-dd") != "0001-01-01" && mSelectedEntity.LastMaintainDate.ToString("yyyy-MM-dd") != "1900-01-01")
            {
                dtpLastMaintainDate.EditValue = mSelectedEntity.LastMaintainDate;
            }
            else
            {
                dtpLastMaintainDate.EditValue = null;
            }
            if (mSelectedEntity.NextMaintainDate.ToString("yyyy-MM-dd") != "0001-01-01" && mSelectedEntity.NextMaintainDate.ToString("yyyy-MM-dd") != "1900-01-01")
            {
                dtpNextMaintainDate.EditValue = mSelectedEntity.NextMaintainDate;
            }
            else
            {
                dtpNextMaintainDate.EditValue = null;
            }
            txtNextMaintainItem.Text = mSelectedEntity.NextMaintainItem;
        }

        private void SetEntity()
        {
            mSelectedEntity.AssetId = null;
            if (txtAsset.Tag != null && txtAsset.Tag.GetType() == typeof(FaAssetsEntity))
            {
                mSelectedEntity.AssetId = ((FaAssetsEntity)txtAsset.Tag).AssetId;
            }
            mSelectedEntity.LastMaintainPerson = txtLastMaintainPerson.Text;
            mSelectedEntity.LastMaintainItem = txtLastMaintainItem.Text;
            mSelectedEntity.LastMaintainFee = CommonFunctions.ToDecimal(txtLastMaintainFee.Text);
            mSelectedEntity.LastMaintainCompany = txtLastMaintainCompany.Text;
            mSelectedEntity.Remark = txtRemark.Text;
            if (dtpLastMaintainDate.EditValue!=null)
            {
                mSelectedEntity.LastMaintainDate = Convert.ToDateTime(dtpLastMaintainDate.EditValue);
            }
            else
            {
                mSelectedEntity.LastMaintainDate = Convert.ToDateTime("1900-01-01");
            }
            if (dtpNextMaintainDate.EditValue!=null)
            {
                mSelectedEntity.NextMaintainDate = Convert.ToDateTime(dtpNextMaintainDate.EditValue);
            }
            else
            {
                mSelectedEntity.NextMaintainDate = Convert.ToDateTime("1900-01-01");
            }
            mSelectedEntity.NextMaintainItem = txtNextMaintainItem.Text;
        }

        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtAsset.Text))
            {
                MessageBox.Show("固定资产不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtLastMaintainPerson.Text))
            {
                MessageBox.Show("保养人不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (dtpLastMaintainDate.EditValue ==null)
            {
                MessageBox.Show("保养日期不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }

        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                SetEntity();
                if (ThisActionType == ActionType.New)
                {
                    bool isSuccessful = FaManufacturerBs.Save(mIsNew, mSelectedEntity);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("保养失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
            //    else if (ThisActionType == ActionType.Modify)
            //    {
            //        bool isSuccessful = FaManufacturerBs.Save(mIsNew, mSelectedEntity);
            //        if (!isSuccessful)
            //        {
            //            MessageBox.Show("保养失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //            return false;
            //        }
            //    }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        #region Events
        private void frmMaintainAssetMaintenance_Load(object sender, EventArgs e)
        {
            SetFields();
        }

        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void txtAsset_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainAssetQuery frm = new frmMaintainAssetQuery();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK && frm.SelectedAsset != null)
            {
                txtAsset.Text = frm.SelectedAsset.AssetCode + " (" + frm.SelectedAsset.AssetName + ")";
                txtAsset.Tag = frm.SelectedAsset;

                mSelectedEntity = FaManufacturerBs.GetByID(frm.SelectedAsset.AssetId);
                if (mSelectedEntity == null)
                {
                    mSelectedEntity = new FaManufacturerEntity();
                    mSelectedEntity.AssetId = frm.SelectedAsset.AssetId;
                    mIsNew = true;
                }
                else
                {
                    mIsNew = false;
                }
            }
        }
        
        #endregion

    }
}